package com.cskaoyan.service.configservice;

import com.cskaoyan.bean.configbean.Config;
import com.cskaoyan.bean.configbean.ConfigExample;
import com.cskaoyan.mapper.configmapper.ConfigMapper;
import com.cskaoyan.vo.configvo.ConfigExpressVo;
import com.cskaoyan.vo.configvo.ConfigMallVo;
import com.cskaoyan.vo.configvo.ConfigOrderVo;
import com.cskaoyan.vo.configvo.ConfigWxVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ked
 * @createTime 2021年05月10日 10:19:00
 */

@Service
public class ConfigServiceImpl implements ConfigService {

    @Resource
    ConfigMapper configMapper;

    @Override
    public ConfigMallVo queryMall() {

        List<Config> configs = configMapper.selectMallByName();
        ConfigMallVo configMallVo = new ConfigMallVo();
        for(Config config: configs){
            if(config.getKeyName().contains("address")){
                configMallVo.setCskaoyanmall_mall_address(config.getKeyValue());
            }
            if(config.getKeyName().contains("name")){
                configMallVo.setCskaoyanmall_mall_name(config.getKeyValue());
            }
            if(config.getKeyName().contains("phone")){
                configMallVo.setCskaoyanmall_mall_phone(config.getKeyValue());
            }
            if(config.getKeyName().contains("qq")){
                configMallVo.setCskaoyanmall_mall_qq(config.getKeyValue());
            }
        }
        return configMallVo;
    }

    @Override
    public void updateMall(ConfigMallVo configMallVo) {
        Config config = new Config();
        Config config2 = new Config();
        Config config3 = new Config();
        Config config4 = new Config();
        config.setKeyName("cskaoyanmall_mall_address");
        config.setKeyValue(configMallVo.getCskaoyanmall_mall_address());
        config.setUpdateTime(new Date());
        config2.setKeyName("cskaoyanmall_mall_name");
        config2.setKeyValue(configMallVo.getCskaoyanmall_mall_name());
        config2.setUpdateTime(new Date());
        config3.setKeyName("cskaoyanmall_mall_phone");
        config3.setKeyValue(configMallVo.getCskaoyanmall_mall_phone());
        config3.setUpdateTime(new Date());
        config4.setKeyName("cskaoyanmall_mall_qq");
        config4.setKeyValue(configMallVo.getCskaoyanmall_mall_qq());
        config4.setUpdateTime(new Date());

        configMapper.updateMallConfigAddress(config);
        configMapper.updateMallConfigName(config2);
        configMapper.updateMallConfigPhone(config3);
        configMapper.updateMallConfigQq(config4);


    }

    @Override
    public ConfigExpressVo queryExpress() {

        ConfigExample configExample = new ConfigExample();
        ConfigExample.Criteria criteria = configExample.createCriteria();
        criteria.andKeyNameLike("%" + "express"+ "%");

        List<Config> configs = configMapper.selectByExample(configExample);
        ConfigExpressVo configMallVo = new ConfigExpressVo();

        for(Config config:configs){

            if(config.getKeyName().contains("min")){
                configMallVo.setCskaoyanmall_express_freight_min(config.getKeyValue());
            }
            if(config.getKeyName().contains("value")){
                configMallVo.setCskaoyanmall_express_freight_value(config.getKeyValue());
            }
        }
        return configMallVo;
    }

    @Override
    public void updateExpress(ConfigExpressVo configExpressVo) {
        Config config = new Config();
        Config config2 = new Config();
        config.setKeyName("cskaoyan_mall_express_freight_value");
        config.setKeyValue(configExpressVo.getCskaoyanmall_express_freight_value());
        config.setUpdateTime(new Date());
        config2.setKeyName("cskaoyan_mall_express_freight_min");
        config2.setKeyValue(configExpressVo.getCskaoyanmall_express_freight_min());
        config2.setUpdateTime(new Date());

        configMapper.updateExpressConfigValue(config);
        configMapper.updateExpressConfigMin(config2);

    }

    @Override
    public ConfigOrderVo queryOrder() {

        ConfigExample configExample = new ConfigExample();
        ConfigExample.Criteria criteria = configExample.createCriteria();
        criteria.andKeyNameLike("%" + "order"+ "%");

        List<Config> configs = configMapper.selectByExample(configExample);
        ConfigOrderVo configOrderVo = new ConfigOrderVo();

        for(Config config:configs){

            if(config.getKeyName().contains("comment")){
                configOrderVo.setCskaoyanmall_order_comment(config.getKeyValue());
            }
            if(config.getKeyName().contains("unconfirm")){
                configOrderVo.setCskaoyanmall_order_unconfirm(config.getKeyValue());
            }
            if(config.getKeyName().contains("unpaid")){
                configOrderVo.setCskaoyanmall_order_unpaid(config.getKeyValue());
            }
        }
        return configOrderVo;
    }

    @Override
    public void updateOrder(ConfigOrderVo configOrderVo) {
        Config config = new Config();
        Config config2 = new Config();
        Config config3 = new Config();
        config.setKeyName("cskaoyanmall_order_comment");
        config.setKeyValue(configOrderVo.getCskaoyanmall_order_comment());
        config.setUpdateTime(new Date());
        config2.setKeyName("cskaoyanmall_order_unconfirm");
        config2.setKeyValue(configOrderVo.getCskaoyanmall_order_unconfirm());
        config2.setUpdateTime(new Date());
        config3.setKeyName("cskaoyanmall_order_unpaid");
        config3.setKeyValue(configOrderVo.getCskaoyanmall_order_unpaid());
        config3.setUpdateTime(new Date());
        configMapper.updateOrderConfigComment(config);
        configMapper.updateOrderConfigUnconfirm(config2);
        configMapper.updateOrderConfigUnpaid(config3);
    }

    @Override
    public ConfigWxVo queryWx() {

        ConfigExample configExample = new ConfigExample();
        ConfigExample.Criteria criteria = configExample.createCriteria();
        criteria.andKeyNameLike("%" + "wx"+ "%");

        List<Config> configs = configMapper.selectByExample(configExample);
        ConfigWxVo configWxVo = new ConfigWxVo();

        for(Config config:configs){
            if(config.getKeyName().contains("goods")){
                configWxVo.setCskaoyanmall_wx_catlog_goods(config.getKeyValue());
            }
            if(config.getKeyName().contains("list")){
                configWxVo.setCskaoyanmall_wx_catlog_list(config.getKeyValue());
            }
            if(config.getKeyName().contains("brand")){
                configWxVo.setCskaoyanmall_wx_index_brand(config.getKeyValue());
            }
            if(config.getKeyName().contains("hot")){
                configWxVo.setCskaoyanmall_wx_index_hot(config.getKeyValue());
            }
            if(config.getKeyName().contains("new")){
                configWxVo.setCskaoyanmall_wx_index_new(config.getKeyValue());
            }
            if(config.getKeyName().contains("topic")){
                configWxVo.setCskaoyanmall_wx_index_topic(config.getKeyValue());
            }
            if(config.getKeyName().contains("share")){
                configWxVo.setCskaoyanmall_wx_share(config.getKeyValue());
            }
        }
        return configWxVo;
    }

    @Override
    public void updateWx(ConfigWxVo configWxVo) {
        Config config = new Config();
        Config config2 = new Config();
        Config config3 = new Config();
        Config config4 = new Config();
        Config config5 = new Config();
        Config config6 = new Config();
        Config config7 = new Config();
        config.setKeyName("cskaoyanmall_wx_catlog_goods");
        config.setKeyValue(configWxVo.getCskaoyanmall_wx_catlog_goods());
        config.setUpdateTime(new Date());
        config2.setKeyName("cskaoyanmall_wx_catlog_list");
        config2.setKeyValue(configWxVo.getCskaoyanmall_wx_catlog_list());
        config2.setUpdateTime(new Date());
        config3.setKeyName("cskaoyanmall_wx_index_brand");
        config3.setKeyValue(configWxVo.getCskaoyanmall_wx_index_brand());
        config3.setUpdateTime(new Date());
        config4.setKeyName("cskaoyanmall_wx_index_hot");
        config4.setKeyValue(configWxVo.getCskaoyanmall_wx_index_hot());
        config4.setUpdateTime(new Date());
        config5.setKeyName("cskaoyanmall_wx_index_new");
        config5.setKeyValue(configWxVo.getCskaoyanmall_wx_index_new());
        config5.setUpdateTime(new Date());
        config6.setKeyName("cskaoyanmall_wx_index_topic");
        config6.setKeyValue(configWxVo.getCskaoyanmall_wx_index_topic());
        config6.setUpdateTime(new Date());
        config7.setKeyName("cskaoyanmall_wx_share");
        config7.setKeyValue(configWxVo.getCskaoyanmall_wx_share());
        config7.setUpdateTime(new Date());
        configMapper.updateWxConfigGoods(config);
        configMapper.updateWxConfigList(config2);
        configMapper.updateWxConfigBrand(config3);
        configMapper.updateWxConfigHot(config4);
        configMapper.updateWxConfigNew(config5);
        configMapper.updateWxConfigTopic(config6);
        configMapper.updateWxConfigShare(config7);
    }



}
